package com.administrator.scoreManageSys.controller.organization;

import com.administrator.scoreManageSys.service.OrganizationService;
import com.administrator.scoreManageSys.service.StudentService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;

/**
 * @author zsh
 * @date 2022/04/06/ 11:19
 */
@RestController
@RequestMapping(value = "/organization/management")
public class OrganizationController {
    @Resource
    private OrganizationService organizationService;

    @PostMapping(value = "/UserAccountList")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "systemUser:list", logical = Logical.OR)
    public JSONObject userAccountList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            Long roleId = data.getLong("roleId");
            Long phone = data.getLong("phone");
            String name = data.getString("name");
            return organizationService.userAccountList(pageNum, pageSize, roleId, phone, name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取用户账户列表 异常");
        }
    }

    @PostMapping(value = "/userAccountAdd")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "systemUser:add", logical = Logical.OR)
    public JSONObject userAccountAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作人id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            String name = data.getString("name");
            Long phone = data.getLong("phone");
            Long identity = data.getLong("identity");
            Long roleId = data.getLong("roleId");
            Long sex = data.getLong("sex");
            Long classId = data.getLong("classId");
            String identityCard = data.getString("identityCard");
            String idEntityCardAddress = data.getString("idEntityCardAddress");
            String address = data.getString("address");
            Long admissionTime = data.getLong("admissionTime");
            return organizationService.userAccountAdd(userId, name, phone, identity, roleId, sex, classId, identityCard, idEntityCardAddress, address, admissionTime);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加用户账户 异常");
        }
    }

    @PostMapping(value = "/resetPasswordProtection")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "systemUser:add", logical = Logical.OR)
    public JSONObject resetPasswordProtection(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return organizationService.resetPasswordProtection(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("重置密保 异常");
        }
    }

    @PostMapping(value = "/resetPassword")
    @RequiresRoles(value = {"authority","studentInfo"})
    @RequiresPermissions(value = {"systemUser:add","member:edit"}, logical = Logical.OR)
    public JSONObject resetPassword(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return organizationService.resetPassword(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("重置密码 异常");
        }
    }

    @PostMapping(value = "/userAccountUpdate")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "systemUser:edit", logical = Logical.OR)
    public JSONObject userAccountUpdate(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            String name = data.getString("name");
            Long phone = data.getLong("phone");
            Long identity = data.getLong("identity");
            Long roleId = data.getLong("roleId");
            Long sex = data.getLong("sex");
            Long classId = data.getLong("classId");
            String identityCard = data.getString("identityCard");
            String idEntityCardAddress = data.getString("idEntityCardAddress");
            String address = data.getString("address");
            Long admissionTime = data.getLong("admissionTime");
            Long oldClassId = data.getLong("oldClassId");
            return organizationService.userAccountUpdate(name, phone, roleId, classId, address,id,oldClassId,identity,sex,identityCard,idEntityCardAddress,admissionTime);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改用户账户 异常");
        }
    }

    @PostMapping(value = "/exportUserList")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "systemUser:list", logical = Logical.OR)
    public void exportUserList(@RequestBody JSONObject data, HttpServletResponse response) {
        try {
            Long roleId = data.getLong("roleId");
            Long phone = data.getLong("phone");
            String name = data.getString("name");
             organizationService.exportUserList(name, phone, roleId,response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出用户信息 异常");
        }
    }

    @PostMapping(value = "/deleteAccount")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "systemUser:add", logical = Logical.OR)
    public JSONObject deleteAccount(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return organizationService.deleteAccount(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除账户 异常");
        }
    }

    @PostMapping(value = "/getUserDetail")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "systemUser:edit", logical = Logical.OR)
    public JSONObject getUserDetail(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return organizationService.getUserDetail(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取用户详情信息 异常");
        }
    }
}
